<?php

namespace App\Http\Controllers\backend;

use Illuminate\Support\Facades\Redirect;
use App\Models\Link;
use Illuminate\Support\Facades\Input;

class LinkController extends BaseController {
	
	public function index() {
		$links = Link::orderBy('order')->get();
		return view('backend.links', compact('links'));
	}
	
	public function create() {
		$link = new Link();
		return view('backend.link_form', compact('link'));
	}
	
	public function store() {
		$input = Input::all();
		$validation = validator($input, Link::$rules, Link::$msgs);
		if($validation->passes()) {
			Link::create($input);
			return redirect('backend/links');
		}
		return Redirect::back()
				->withInput()
				->withErrors($validation);
	}
	
	public function edit($id) {
		$link = Link::find($id);
		return view('backend.link_form', compact('link'));
	}
	
	public function update($id) {
		$input = Input::all();
		$validation = validator($input, Link::$rules, Link::$msgs);
		if($validation->passes()) {
			$link = Link::find($id);
			$link->update($input);
			$link->save();
			return redirect('backend/links');
		}
		return Redirect::back()
				->withInput()
				->withErrors($validation);
	}
	
	public function destroy($id) {
		$link = Link::find($id);
		$link->delete();
		return redirect('backend/links');
	}
}
